package com.congee02.dbservlet.service.login;

import com.congee02.dbservlet.dao.UserDao;
import com.congee02.dbservlet.db.SingletonJdbcTemplate;
import com.congee02.dbservlet.framework.ioc.annotation.Autowired;
import com.congee02.dbservlet.framework.ioc.annotation.Component;
import com.congee02.dbservlet.result.Result;
import com.congee02.dbservlet.result.ResultFactory;
import com.congee02.dbservlet.utils.StringValidationUtils;

import java.sql.SQLException;

@Component("loginService")
public class LoginServiceImpl implements LoginService {

    @Autowired
    private UserDao userDao;

    @Override
    public Result<?> login(String username, String pwd) {
        if (StringValidationUtils.isAnyNullOrBlank(username, pwd)) {
            return ResultFactory.failedResult("用户名和密码不能为空。");
        }
        boolean userExist = false;
        try {
            userExist = userDao.isUserExist(username, pwd, SingletonJdbcTemplate.get());
        } catch (SQLException e) {
            return ResultFactory.failedResult("数据库未知错误。");
        }
        if (userExist) {
            return ResultFactory.successResult("成功登录");
        } else {
            return ResultFactory.failedResult("用户名不存在或者密码不匹配");
        }
    }

}
